home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 481 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: Mark Russell <M.T.Russell@ukc.ac.uk>
  3. Newsgroups: comp.std.c++
  4. Subject: Article for comp.std.c++: Eliminating #ifdef: if const
  5. Date: 16 Feb 1996 09:03:51 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <199602160807.IAA06126@condor.ukc.ac.uk>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Fri, 16 Feb 1996 08:07:47 +0000
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBVAwUBMSS5D0y4NqrwXLNJAQHCMgIAwfzdWgWPjg1FTLNyzj5jBN30uwRyb5EW
  13.     hu4HHUL2176dHKNZ8uJWUlUJ81tzJVgIGL0y8dKhfGNir0AEgoYhTA==
  14.     =//uW
  15. Originator: austern@isolde.mti.sgi.com
  16.  
  17. Subject: Eliminating #ifdef: if const
  18.  
  19. Here's an idea I had for a non-preprocessor replacement for #if and  #ifdef.
  20.  
  21.     if const (<expr>) { <stuff> }
  22.  
  23. `if const' is a compile time version of `if'.  <expr> is a constant
  24. expression which is convertible to bool.  If it yields true, <stuff>
  25. is processed as normal.  Otherwise <stuff> is parsed into tokens, but
  26. ignored except for counting { and } nesting.  <stuff> is anything that
  27. can appear inside a namespace block.  `if const' does not introduce a
  28. new scope.
  29.  
  30. The following is also legal, with the obvious meaning:
  31.  
  32.     if const (<expr>) { <stuff> } else const { <other stuff> }
  33.  
  34. There are also a number of compile-time pseudo-functions (I hesitate
  35. to call them pragmas):
  36.  
  37.     bool #os_version(const char* str)
  38.  
  39.         os version (uname -r) at or later than str
  40.  
  41.     bool #os_name(const char *str)
  42.  
  43.         os name (uname -s) is str
  44.  
  45.     bool #compiler_name(const char *)
  46.     bool #compiler_version(const char*)
  47.  
  48.         Same tests for the compiler
  49.  
  50.     bool #header_exists(const char* path)
  51.  
  52.         Would `#include <path>' succeed?
  53.  
  54.     bool #have_test(const char* varname)
  55.  
  56.         Would #varname(str) work (this would be useful to
  57.         allow testing for implementation defined tests.
  58.  
  59. Using these you could do things like this:
  60.  
  61.     // Are we running IRIX 5.3
  62.     const bool using_irix_53 = #os_name("IRIX") &&
  63.                    #os_version("5.3") && !#os_version("5.4")
  64.  
  65.     if const (using_irix_53) {
  66.         // Do some stuff needed just for IRIX 5.3
  67.     }
  68.  
  69.     if const (#compiler("g++")) {
  70.         // Do some g++ stuff
  71.     }
  72.  
  73.     if const (#header_exists("sys/frobnitz9000.h")) {
  74.         #include "sys/frobnitz9000.h"
  75.         // Do stuff for the Frobnitz 9000
  76.     }
  77.  
  78.     if const (#have_test("extension")) {
  79.         const bool have_foo_extension = #extension_exists("foo");
  80.     }
  81.     else const {
  82.         const bool have_foo_extension = false;
  83.     }
  84.  
  85.     if const (have_foo_extension) {
  86.         // Stuff using the `foo' extension
  87.     }
  88.  
  89. With this I could personally stop using the preprocessor for anything
  90. except #include.  If `include' (as described in D&E) is also
  91. implemented, I could stop using the preprocessor altogether, which
  92. would be nice.
  93.  
  94. I realise this is too late for the upcoming version of the standard,
  95. but I think it would be a useful thing for a subsequent version.
  96.  
  97. Comments?
  98.  
  99. Mark Russell
  100. ---
  101. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  102.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  103.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  104.